/* 
 * oms，一个基于J2EE架构运营支撑平台
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.publish.impl;

import java.util.Date;
import java.util.List;

import org.grayrabbit.cms.dao.publish.PublishLogDao;
import org.grayrabbit.cms.dao.publish.PublishTaskDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.OperLog;
import org.grayrabbit.cms.entity.PublishLog;
import org.grayrabbit.cms.entity.PublishTask;
import org.grayrabbit.cms.service.publish.PublishTaskService;
import org.grayrabbit.cms.vo.publish.PublishTaskBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：

 * @version 1.0,创建时间: 2012-01-05 17:51:15  
 */
@Service
@Transactional
public class PublishTaskServiceImpl extends GenericServiceImpl implements PublishTaskService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("publishTaskDaoImpl")
	private PublishTaskDao publishTaskDao;
	
	@Autowired
	@Qualifier("publishLogDaoImpl")
	private PublishLogDao publishLogDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see PublishTaskService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public PublishTask getPublishTask(final String taskId){
		PublishTask publishTask = publishTaskDao.findById(taskId);
		publishTaskDao.initialize(publishTask);
		return publishTask;
	}
	
	/**
	 * @see PublishTaskService#delete(String)
	 */
	@Transactional
	public void delete(final PublishTask publishTask) {
		publishTaskDao.makeTransient(publishTask);
	}
	
	/**
	 * @see PublishTaskService#save(OperLog)
	 */
	@Transactional
	public void save(final PublishTask publishTask) {
		publishTaskDao.merge(publishTask);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see PublishTaskService#getPage(PublishTaskBean)
	 */
	@Transactional(readOnly=true)
	public Page<PublishTask> getPage(final PublishTaskBean publishTaskBean){
		Page<PublishTask> page =  new Page<PublishTask>();
		page.setTotal(publishTaskDao.count(publishTaskBean));
		page.setResult(publishTaskDao.findByQc(publishTaskDao.assembly(publishTaskBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see PublishTaskService#list(Page, PublishTaskBean)
	 */
	public void list(Page<PublishTask> page, PublishTaskBean publishTaskBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(publishTaskDao.count(publishTaskBean));
		}
		page.setResult(publishTaskDao.findByPage(page,publishTaskBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see PublishTaskService#delete(String[])
	 */
	@Transactional
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			publishTaskDao.makeTransient(publishTaskDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				publishTaskDao.clear();
		}
		
	}

	@Transactional
	public PublishTask create(Admin currentAdmin,String taskName,String taskDesc,Boolean isAuto) {
		PublishTask pt = new PublishTask();
		pt.setTaskBeginTime(new Date());
		pt.setIsAutoPublish(isAuto);
		pt.setTaskName(taskName);
		pt.setTaskDesc(taskDesc);
		if(isAuto!=null && isAuto.booleanValue()){//自动发布时,不记录操作人
			pt.setTaskOperLoginName(currentAdmin.getLoginName());
			pt.setTaskOperRealName(currentAdmin.getRealName());
		}else{
			pt.setTaskOperLoginName("系统自动");
			pt.setTaskOperRealName("系统自动");
		}
		pt.setTaskStatus(PublishTask.STATUS_NEW);//新建任务
		publishTaskDao.makePersistent(pt);
		return pt;
	}

	public List<PublishLog> readPublishLogs(String taskId, int offset) {
		return publishLogDao.findByTaskId(taskId, offset);
	}
 
}